---
title: "Twitter Data Framing Analysis"
author: "G. Agustin Markarian"
date: "8/6/2021"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r}
#Packages
library(readr)
library(readxl)
library(RColorBrewer)
library(gridExtra)
library(wordcloud)
library(sjmisc)
library(ggplot2)
library(sandwich)
library(lmtest)
library(clubSandwich)
library(stargazer)
library(schoolmath)
library(tidyverse)
library(car)
library(multiwayvcov)
library(ggeffects)
library(miceadds)
library(dotwhisker)
```

```{r}
#Aggregate to legislator level
d_agg <- read.csv("Aggregated_Twitter_Data_Markarian.csv")
```

#Aggregate Pre-post test

```{r}
m1 <- lm(mention ~ post_whitefat + post_nonwhitefat  + dem + State + as.factor(year), data = d_agg)
m1.se <- coeftest(m1, vcov = vcovCL, cluster = ~case_number, type = "HC1")

m2 <- lm(political ~ post_whitefat + post_nonwhitefat  + dem + State + as.factor(year), data = d_agg)
m2.se <- coeftest(m2, vcov = vcovCL, cluster = ~case_number, type = "HC1")
```

```{r}
m3 <- lm(mention_ra ~ post_whitefat + post_nonwhitefat  + dem + State + as.factor(year), data = d_agg)
m3.se <- coeftest(m2, vcov = vcovCL, cluster = ~case_number, type = "HC1")

m4 <- lm(political_ra ~ post_whitefat + post_nonwhitefat  + dem + State + as.factor(year), data = d_agg)
m4.se <- coeftest(m4, vcov = vcovCL, cluster = ~case_number, type = "HC1")

```

```{r}
m5 <- lm(mention ~ post_whitefat + post_nonwhitefat  + handle + as.factor(year), data = d_agg)
m5.se <- coeftest(m5, vcov = vcovCL, cluster = ~case_number, type = "HC1")

m6 <- lm(political ~ post_whitefat + post_nonwhitefat  + handle + as.factor(year), data = d_agg)
m6.se <- coeftest(m6, vcov = vcovCL, cluster = ~case_number, type = "HC1")

```

## Coef Plots

```{r}
stargazer(m1, m3, m5, se = list(m1.se[,2], m3.se[,2], m5.se[,2]),  title = "", dep.var.labels = c("State FEs Model", "Second Coder Model", "Legislator FEs Model"),  omit= c("State", "year", "handle"), omit.labels = c("State FEs", "Year FEs", "Legislator FEs"),   column.sep.width = "1pt", font.size = "small", header=FALSE, float = FALSE,  star.char = c("+", "*", "**", "***"),  star.cutoffs = c(0.1, 0.05, 0.01, 0.001),  notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"),  notes.append = F, out="table1_msframes_apr23.doc", omit.stat = c("f", "ser"), type = "html" )
```

```{r}

stargazer(m2, m4, m6, se = list(m2.se[,2], m4.se[,2], m6.se[,2]),  title = "", dep.var.labels = c("State FEs Model", "Second Coder Model", "Legislator FEs Model"),  omit= c("State", "year", "handle"), omit.labels = c("State FEs", "Year FEs", "Legislator FEs"),   column.sep.width = "1pt", font.size = "small", header=FALSE, float = FALSE,  star.char = c("+", "*", "**", "***"),  star.cutoffs = c(0.1, 0.05, 0.01, 0.001),  notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"),  notes.append = F, out="table2_msframes_apr23.doc", omit.stat = c("f", "ser"), type = "html" )
```

```{r}
#Plot
m1df <- broom::tidy(m1) %>% 
  filter(term %in% c("post_whitefat","post_nonwhitefat")) %>%
  mutate(model = "Model 1",
         std.error = m1.se[c(2:3),2])

m2df <- broom::tidy(m2) %>% 
  filter(term %in% c("post_whitefat","post_nonwhitefat")) %>%
  mutate(model = "Model 2",
         std.error = m2.se[c(2:3),2])

m3df <- broom::tidy(m3) %>% 
  filter(term %in% c("post_whitefat","post_nonwhitefat")) %>%
  mutate(model = "Model 3",
         std.error = m3.se[c(2:3),2])

m4df <- broom::tidy(m4) %>% 
  filter(term %in% c("post_whitefat","post_nonwhitefat")) %>%
  mutate(model = "Model 4",
         std.error = m4.se[c(2:3),2])

m5df <- broom::tidy(m5) %>% 
  filter(term %in% c("post_whitefat","post_nonwhitefat")) %>%
  mutate(model = "Model 5",
         std.error = m5.se[c(2:3),2])

m6df <- broom::tidy(m6) %>% 
  filter(term %in% c("post_whitefat","post_nonwhitefat")) %>%
  mutate(model = "Model 6",
         std.error = m6.se[c(2:3),2])


models1 <- rbind(m1df, m3df, m5df) %>%
  relabel_predictors("post_whitefat" = "Number of\nWhite Fatalities",
                     "post_nonwhitefat" = "Number of\nREM Fatalities")

models2 <- rbind(m2df, m4df, m6df) %>%
  relabel_predictors("post_whitefat" = "Number of\nWhite Fatalities",
                     "post_nonwhitefat" = "Number\nof REM Fatalities")

models1$model <- factor(models1$model, levels = c("Model 1", "Model 3", "Model 5"))

g1 <- dwplot(models1, ci = 0.95,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2
       ), dot_args = list(aes(shape = model))) +
    theme_bw(base_size = 11) + xlab("") + ylab("") +
    theme(
        plot.title = element_text(face = "bold"),
        legend.position = c(0.007, 0.01),
        legend.justification = c(0, 0),
        legend.background = element_rect(colour = "grey80"),
        legend.title.align = .5
    ) +
    labs(subtitle = "Number of Gun Violence\nTweets") +
    scale_colour_grey(
        start = .1,
        end = .7,
        breaks = c("Model 1", "Model 3", "Model 5"),
        name = "Model",
        labels = c("State FEs + Year FEs", "State FEs + Year FEs\nSecond Coder", "Legislator FEs + Year FEs")
    ) +
    scale_shape_discrete(
          breaks = c("Model 1", "Model 3", "Model 5"),
        name = "Model",
        labels = c("State FEs + Year FEs", "State FEs + Year FEs\nSecond Coder", "Legislator FEs + Year FEs")
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.direction="horizontal")


g2 <- dwplot(models2, ci = 0.95,
       vline = geom_vline(
         xintercept = 0,
         colour = "grey60",
         linetype = 2
       ), dot_args = list(aes(shape = model))) +
  theme_bw(base_size = 11) + xlab("") + ylab("") +
  labs(subtitle = "Number of Politicized Gun Violence\nTweets") +
    scale_colour_grey(
        start = .1,
        end = .7,
        breaks = c("Model 2", "Model 4", "Model 6"),
        name = "Model",
        labels = c("State FEs + Year FEs", "State FEs + Year FEs\nSecond Coder", "Legislator FEs + Year FEs")
    ) +
    scale_shape_discrete(
          breaks = c("Model 2", "Model 4", "Model 6"),
        name = "Model",
        labels = c("State FEs + Year FEs", "State FEs + Year FEs\nSecond Coder", "Legislator FEs + Year FEs")
    ) +
    guides(
        shape = guide_legend(""), 
        colour = guide_legend("")) +
        theme(legend.position = "none", axis.text.y = element_blank())



g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(g1)

g1 <- g1 + theme(legend.position="none")

lay = rbind(c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(1,1,1,1,2,2,2),
            c(NA,4,4,4,NA))

p3 <- grid.arrange(g1,g2, mylegend, layout_matrix=lay)

p3
```


#Interactipon Test

```{r}
m1 <- lm(mention ~ post_totalfat*prop.white  + dem + State + as.factor(year), data = d_agg)
m1.se <- coeftest(m1, vcov = vcovCL, cluster = ~case_number, type = "HC1")

m2 <- lm(political ~ post_totalfat*prop.white  + dem + State + as.factor(year), data = d_agg)
m2.se <- coeftest(m2, vcov = vcovCL, cluster = ~case_number, type = "HC1")
```

```{r}
m3 <- lm(mention_ra ~ post_totalfat*prop.white  + dem + State + as.factor(year), data = d_agg)
m3.se <- coeftest(m3, vcov = vcovCL, cluster = ~case_number, type = "HC1")

m4 <- lm(political_ra ~ post_totalfat*prop.white  + dem + State + as.factor(year), data = d_agg)
m4.se <- coeftest(m4, vcov = vcovCL, cluster = ~case_number, type = "HC1")

```

```{r}
m5 <- lm(mention ~ post_totalfat*prop.white  + handle + as.factor(year), data = d_agg)
m5.se <- coeftest(m5, vcov = vcovCL, cluster = ~case_number, type = "HC1")

m6 <- lm(political ~ post_totalfat*prop.white  + handle + as.factor(year), data = d_agg)
m6.se <- coeftest(m6, vcov = vcovCL, cluster = ~case_number, type = "HC1")

```

```{r}
stargazer(m1, m3, m5, se = list(m1.se[,2], m3.se[,2], m5.se[,2]),  title = "", dep.var.labels = c("State FEs Model", "Second Coder Model", "Legislator FEs Model"),  omit= c("State", "year", "handle"), omit.labels = c("State FEs", "Year FEs", "Legislator FEs"),   column.sep.width = "1pt", font.size = "small", header=FALSE, float = FALSE,  star.char = c("+", "*", "**", "***"),  star.cutoffs = c(0.1, 0.05, 0.01, 0.001),  notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"),  notes.append = F, out="table1SI_msframes_apr23.doc", omit.stat = c("f", "ser"), type = "html" )
```

```{r}

stargazer(m2, m4, m6, se = list(m2.se[,2], m4.se[,2], m6.se[,2]),  title = "", dep.var.labels = c("State FEs Model", "Second Coder Model", "Legislator FEs Model"),  omit= c("State", "year", "handle"), omit.labels = c("State FEs", "Year FEs", "Legislator FEs"),   column.sep.width = "1pt", font.size = "small", header=FALSE, float = FALSE,  star.char = c("+", "*", "**", "***"),  star.cutoffs = c(0.1, 0.05, 0.01, 0.001),  notes = c("+ p<0.1; * p<0.05; ** p<0.01; *** p<0.001"),  notes.append = F, out="table2SI_msframes_apr23.doc", omit.stat = c("f", "ser"), type = "html" )
```


